package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.edu.domain.EduVideo;
import com.ruoyi.edu.service.IEduVideoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程视频Controller
 * 
 * @author niruichen
 * @date 2020-12-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/edu/video" )
public class EduVideoController extends BaseController {

    private final IEduVideoService iEduVideoService;

    /**
     * 查询课程视频列表
     */
    @PreAuthorize("@ss.hasPermi('edu:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduVideo eduVideo) {
        startPage();
        List<EduVideo> list = iEduVideoService.queryList(eduVideo);
        return getDataTable(list);
    }

    /**
     * 导出课程视频列表
     */
    @PreAuthorize("@ss.hasPermi('edu:video:export')" )
    @Log(title = "课程视频" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(EduVideo eduVideo) {
        List<EduVideo> list = iEduVideoService.queryList(eduVideo);
        ExcelUtil<EduVideo> util = new ExcelUtil<EduVideo>(EduVideo.class);
        return util.exportExcel(list, "video" );
    }

    /**
     * 获取课程视频详细信息
     */
    @PreAuthorize("@ss.hasPermi('edu:video:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iEduVideoService.getById(id));
    }

    /**
     * 新增课程视频
     */
    @PreAuthorize("@ss.hasPermi('edu:video:add')" )
    @Log(title = "课程视频" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduVideo eduVideo) {
        return toAjax(iEduVideoService.save(eduVideo) ? 1 : 0);
    }

    /**
     * 修改课程视频
     */
    @PreAuthorize("@ss.hasPermi('edu:video:edit')" )
    @Log(title = "课程视频" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduVideo eduVideo) {
        return toAjax(iEduVideoService.updateById(eduVideo) ? 1 : 0);
    }

    /**
     * 删除课程视频
     * TODO 需要完善，删除小结时要把里面的视频全部删除
     */
    @PreAuthorize("@ss.hasPermi('edu:video:remove')" )
    @Log(title = "课程视频" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}" )
    public AjaxResult remove(@PathVariable String id) {
        return toAjax(iEduVideoService.removeById(id) ? 1 : 0);
    }
}
